import static com.googlecode.javacv.cpp.opencv_core.cvAddWeighted;
import static com.googlecode.javacv.cpp.opencv_core.cvCopy;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSize;
import static com.googlecode.javacv.cpp.opencv_highgui.CV_LOAD_IMAGE_COLOR;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;
import static opencvtest.OpenCVUtils.loadAndShowOrExit;
import static opencvtest.OpenCVUtils.show;
import static opencvtest.OpenCVUtils.toIplROI;

import java.awt.Rectangle;
import java.io.File;

import com.googlecode.javacv.cpp.opencv_core;
import com.googlecode.javacv.cpp.opencv_core.CvMat;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

/**
 * Created with IntelliJ IDEA. User: joey Date: 15/3/14 Time: 下午10:56 To change
 * this template use File | Settings | File Templates.
 */
public class JavaCvRemoveLargeWater {

    public static void main(String[] args) {

        // step. 1 扣出水印
        // opencv_core.IplImage image = loadAndShowOrExit(new File("w1.jpg"),
        // CV_LOAD_IMAGE_COLOR);
        //
        // // Display image with marked ROI
        // Rectangle rect = new Rectangle(100, 100, 110, 32);
        // show(drawOnImage(image, rect, Color.RED), "Input template");
        // image.roi(toIplROI(rect));
        //
        // opencv_core.CvMat mat = image.asCvMat();
        //
        // cvSaveImage("water.jpg", mat);
        //
        //
        // show(mat, "mask roi image");

        // step.2 处理水印底色为黑色
        // opencv_core.IplImage image = loadAndShowOrExit(new File("water.jpg"),
        // CV_LOAD_IMAGE_COLOR);
        // CvMat mat = image.asCvMat();
        //
        // int n = mat.cols() * mat.rows() * mat.nChannels();
        //
        // for (int i = 0; i < n; i++) {
        // int v = (int) mat.get(i);
        //
        // System.out.println(v);
        // if (v >= 230 && v <= 250) {
        // mat.put(i, 255 - v);
        // } else {
        // int tmpI = (int) (Math.ceil(i / 3.0) * 3 - 2);
        // if (i == tmpI) {
        // mat.put(i, 255 - v);
        // } else {
        //
        // mat.put(i, v - 10);
        // }
        // }
        // }
        // System.out.println(mat);
        // cvSaveImage("water-f1.jpg", mat);
        //
        // show(mat, "mask roi image");

        // step.3 尝试去除水印
        // opencv_core.IplImage image = loadAndShowOrExit(new File("w1.jpg"),
        // CV_LOAD_IMAGE_COLOR);
        //
        // Rectangle rect = new Rectangle(100, 100, 110, 32);
        // //show(drawOnImage(image, rect, Color.RED), "Input template");
        // IplImage roiImage = image.roi(toIplROI(rect));
        //
        //
        // IplImage water = loadAndShowOrExit(new File("water-r.jpg"),
        // CV_LOAD_IMAGE_COLOR);
        // CvMat waterMat = water.asCvMat();
        //
        // cvAdd(waterMat, roiImage, image, null);
        //
        // image.roi(null);
        // show(image, "removed water image");
        //
        // cvSaveImage("w1-nw.jpg", image);

        // step. 3 尝试去除其他图片的水印
        opencv_core.IplImage image = loadAndShowOrExit(new File("w2.jpg"), CV_LOAD_IMAGE_COLOR);

        Rectangle rect = new Rectangle(100, 100, 110, 32);
        // show(drawOnImage(image, rect, Color.RED), "Input template");
        IplImage roiImage = image.roi(toIplROI(rect));

        IplImage water = loadAndShowOrExit(new File("water-r.jpg"), CV_LOAD_IMAGE_COLOR);
        CvMat waterMat = water.asCvMat();

        IplImage roiImage2 = IplImage.create(cvGetSize(water), water.depth(), water.nChannels());
        cvCopy(roiImage, roiImage2);

        CvMat roiMat = roiImage2.asCvMat();

        int n = roiMat.rows() * roiMat.cols() * roiMat.nChannels();

        for (int i = 0; i < n; i++) {
            int wv = (int) waterMat.get(i);
            int rv = (int) roiMat.get(i);
            if (wv == 1) {
                System.out.println(wv);
                roiMat.put(i, rv - 25);
            } else {
                // roiMat.put(i, rv + wv);
            }
            // roiMat.put(i, rv + wv);
        }

        show(roiMat, "roi mat");

        // cvAdd(waterMat, roiImage, image, null);
        cvAddWeighted(roiImage2, 1.0, roiImage, 0.0, 0.0, image);

        image.roi(null);
        show(image, "removed water image");

        cvSaveImage("w2-nw.jpg", image);

    }

}
